package fr.cie.blog.filter;

import javax.servlet.ServletException;

import org.apache.log4j.Logger;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;

/**
 * @author Jee3 attaque le filtre OpensessionInViewFilter (voire pattern
 *         hibernate du même nom)
 */
public class MyFilterHib extends OpenSessionInViewFilter {

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(MyFilterHib.class);

	/**
	 * default constructor.
	 */
	public MyFilterHib() {
		super();
		LOG.info("Filter Instance ?  ---------------------------------------");
	}

	@Override
	protected final void initFilterBean() throws ServletException {
		LOG.info("SESSION FLUSH BEFORE set ? --------" + super.getFlushMode());
		LOG.info("SESSION IS OPEN  ---------------" + super.isSingleSession());
		setFlushMode(FlushMode.AUTO);
		LOG.info("SESSION FLUSH TO AUTO  ------------" + super.getFlushMode());

		super.initFilterBean();
	}

	@Override
	protected final void closeSession(final Session session,
			final SessionFactory sessionFactory) {
		LOG.info("Verifié !  in filter  ===> session is closed");
		LOG.info("SESSION FLUSH AFTER op set ?  -------------------"
				+ super.getFlushMode());
		super.closeSession(session, sessionFactory);
	}

}